import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('@/views/login/Login')
  },
  {
    path: '/',
    name: 'Layout',
    redirect:'/home',
    component: ()=>import('@/views/Layout'),
    children:[
      {
        path: 'home',
        name: 'home',
        component: HomeView
      },
      {
        path: 'userList',
        name: 'UserList',
        component: () => import( '@/views/user/User')
      },
      {
        path: 'addUser',
        name: 'addUser',
        component: () => import( '@/views/user/AddUser')
      },
      {
        path: 'editUser',
        name: 'editUser',
        component: () => import( '@/views/user/EditUser')
      },
      {
        path: 'adminList',
        name: 'AdminList',
        component: () => import( '@/views/admin/Admin')
      },
      {
        path: 'addAdmin',
        name: 'addAdmin',
        component: () => import( '@/views/admin/AddAdmin')
      },
      {
        path: 'editAdmin',
        name: 'editAdmin',
        component: () => import( '@/views/admin/EditAdmin')
      },
      {
        path: 'categoryList',
        name: 'CategoryList',
        component: () => import( '@/views/category/Category')
      },
      {
        path: 'addCategory',
        name: 'addCategory',
        component: () => import( '@/views/category/AddCategory')
      },
      {
        path: 'editCategory',
        name: 'editCategory',
        component: () => import( '@/views/category/EditCategory')
      },
      {
        path: 'bookList',
        name: 'BookList',
        component: () => import( '@/views/book/Book')
      },
      {
        path: 'addBook',
        name: 'addBook',
        component: () => import( '@/views/book/AddBook')
      },
      {
        path: 'editBook',
        name: 'editBook',
        component: () => import( '@/views/book/EditBook')
      },
      {
        path: 'borrowList',
        name: 'BorrowList',
        component: () => import( '@/views/borrow/Borrow')
      },
      {
        path: 'addBorrow',
        name: 'addBorrow',
        component: () => import( '@/views/borrow/AddBorrow')
      },
      {
        path: 'editBorrow',
        name: 'editBorrow',
        component: () => import( '@/views/borrow/EditBorrow')
      },
      {
        path: 'returList',
        name: 'ReturList',
        component: () => import( '@/views/retur/Retur')
      }
    ]
  },
  {
    path: '*',
    name: '404',
    component: () => import( '@/views/404')
  },
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') next()
  const admin = Cookies.get("admin")
  if (!admin && to.path !== '/login') return next("/login")  // 强制退回到登录页面
  // 访问 /home 的时候，并且cookie里面存在数据，这个时候我就直接放行
  next()
})



export default router


